import threading
from Tello import myTello
import msvcrt
import cv2

def key_wait(tello):
    print('线程开始')
    n=361
    while True:
        
        
        key = msvcrt.getch()
        if key==b't':
            print('起飞')
            
            tello.takeoff()
        if key==b'w':
            print('向前')
            tello.forward(10)
        if key==b's':
            print('向后')
            tello.backward(10)
        if key==b'a':
            print('向左')
            tello.left(10)
        if key==b'd':
            print('向右')
            tello.right(10)
        if key==b'r':
            tello.rece

        if key==b'p':
            print('悬停')
            tello.left(0)
            tello.right(0)
            tello.up(5)
            tello.down(0)
            tello.forward(0)
            tello.backward(0)

        #     print('j')
        # if key==b'l':
        #     uav.flyCmd("turnright","fast")
        #     print('l')
        if key==b'i':
            print('向上')
            tello.up(20)
        if key==b'k':
            print('向下')
            tello.down(10)
        # if key==b'q':
        #     uav.flyCmd("stop")
        #     t1.time_sub()d
        #     print('q')
        # if key == b'e':
        #     uav.flyCmd("stop")
        #     t2.time_sub()
        if key==b'x':
            
            path_data = 'data/'+str(n).zfill(4)+'.jpg'
            cv2.imwrite(path_data,tello.Tello_frame)
            n+=1
        #     n+=1
        #     print('save pic %s'%path_data)
        # if key ==b'x':
        #     b = uav.client.getVerticalSense()
        #     print(b)
        if key == b'b':
            print('降落')
            tello.land()
            
        if key == b'z':
            print('退出')
            tello.land()
            tello.sock.close()
            tello.quit()
            break
        if key == b'0':
            print('自动降落')
            tello.AUTOLAND=True
            autolanding=threading.Thread(target=tello.autoland)
            autolanding.start()

def main(tello):
    

    t = threading.Thread(target=key_wait,args=(tello,))
    t.setDaemon(True)
    t.start()
    tello.detection()

if __name__=='__main__':
    tello=myTello()

    main(tello)
